<?php

namespace App\Jobs\Stats\User\Elasticsearch;

use App\Jobs\Stats\User\Week as JobStats;
use App\Jobs\Stats\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info;
use App\Models\Elasticsearch\Log\Login;

class Week extends JobStats
{
    use DatabaseElasticsearch;

    /**
     * 游戏人数
     */
    protected function stats_u_game()
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【状态】
        $this->makeWhere($params, 'status', Info::STATUS_NORMAL);
        // 筛选【玩家类型】
        $this->makeWhere($params, 'player_type', Info::PLAYER_TYPE);
        // 筛选【时间】
        $this->makeWhereDatetime($params, 'end_time', [$this->task->timed, $this->task->timed + 7 * 86400], Info::INDEX_NAME);
        // 筛选【平台】
        $this->makeWhere($params, 'plat_code', $this->task->plat_code);

        // 统计参数
        $params['body']['aggs'] = $this->makeDistinct('uid');

        // 查询数据
        return Info::search($params)['aggregations']['distinct_uid']['value'];
    }

    /**
     * 登录人数
     */
    protected function stats_u_sign()
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【时间】
        $this->makeWhereDatetime($params, 'login_time', [$this->task->timed, $this->task->timed + 7 * 86400], Info::INDEX_NAME);
        // 筛选【平台】
        $this->makeWhere($params, 'plat_code', $this->task->plat_code);

        // 统计参数
        $params['body']['aggs'] = $this->makeDistinct('uid');

        // 查询数据
        return Login::search($params)['aggregations']['distinct_uid']['value'];
    }
}
